package fr.umlv.escape_ig.ship;

import fr.umlv.escape_ig.weapon.Ball;
import android.content.Context;
import android.graphics.Canvas;

public class Player extends Robot {

        private final static float INITIAL_RADIUS = 3.0f;

        private Ball loadingBall = null;
        private float ballRadius;

        private final Object ballLock = new Object();

        public Player(Context context) {
                super(context);
        }

        public Player(Context context, float f, float g) {
                super(context, f, g);
        }

        @Override
        public void draw(Canvas canvas) {
                super.draw(canvas);
                
                synchronized (ballLock) {
                        if (loadingBall != null) {
                                loadingBall.draw(canvas);
                        }
                }
        }

        @Override
        public void update() {
                super.update();
                if (loadingBall == null) {
                        return;
                }

                ballRadius += 1.0f;
                float centerX = rect.centerX();
                
                loadingBall.rect.left = centerX - ballRadius;
                loadingBall.rect.top = rect.top - 2.0f*ballRadius;
                loadingBall.rect.right = centerX + ballRadius;
                loadingBall.rect.bottom = rect.top;
        }

        public void loadWeapon() {
                float centerX = rect.centerX();
                ballRadius = INITIAL_RADIUS;
                synchronized (ballLock) {
                        loadingBall = new Ball(centerX - INITIAL_RADIUS, rect.top - 2*INITIAL_RADIUS, 
                                        centerX + INITIAL_RADIUS, rect.top);
                }
        }

        public Ball fire() {
                if (loadingBall == null) {
                        return null;
                }

                synchronized (ballLock ) {
                        Ball b  = loadingBall;
                        loadingBall = null;
                        return b;
                }

        }
}